/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package employeedetails;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;

/**
 *
 * @author TQNINH
 */
public class EmployeeTest {

    public EmployeeTest() {
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String choice = null;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("Enter the details of an Employee: ");
            System.out.print("Name: ");
            String name = input.next();
            System.out.print("Experience: ");
            int experience = input.nextInt();
            System.out.print("Salary: ");
            double salary = input.nextDouble();
            
            if((name.length() > 0) && (experience >= 0) && (salary >= 0.0)) {
                Employee emp = new Employee(name, experience, salary);
                try {
                    FileOutputStream fos = new FileOutputStream("employeeDetails", true);
                    ObjectOutputStream outStream = new ObjectOutputStream(fos);
                    outStream.writeObject(emp);
                    System.out.println("Saved employee details in the file.");
                    outStream.flush();
                    outStream.close();
                    fos.close();
                } catch(FileNotFoundException e) {
                    e.printStackTrace();
                    System.out.println("The file to be copied could not be"
                            + " located!");
                } catch(IOException e) {
                    e.printStackTrace();
                    System.out.println("An error occurred while reading from"
                            + " the file!");
                }
            } else {
                System.out.println("Enter all the employee details correctly!");
            }
            System.out.print("Do you want to continue? (Y/N): ");
            choice = input.next().toUpperCase();
        } while(!choice.equalsIgnoreCase("N"));
    }
}
